/**
 * Student Name: Philippe Gambling
 * Course: CSCI 5733
 * Section: 01
 * Student Number: 0603418
 * Assignment: Homework 4
 * Requirements: http://dcm.uhcl.edu/yue/courses/xml/summer2009/hw/h4.html
 * Filename: CreateFractal.java
 * Descriptions: Executes an XSLT transformation on a base pattern and transform XML.
 * 
 */

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.SAXException;


/**
 * @author philippe
 *
 */
public class CreateFractal {

	/**
	 * @param args
	 */
	public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException,
        SAXException, IOException 
    {
		if(args.length != 4) {
			System.err.println("Usage: CreateFractal <baseFractal SVG> " +
					           "<fractalTransform XML> <generateFractal XSL> <outputFractal SVG>");
		    return;
		}
		
		// Get the parameter value from the command line.
		String baseFractalSvg = args[0];
		String fractalTransformXml = args[1];
		String generateFractalXsl = args[2];
		String outputFractalSvg = args[3];
		
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer(new StreamSource(generateFractalXsl));

		// Set the transform XML parameter
		transformer.setParameter("fractalTransformXml", fractalTransformXml);
		
		transformer.transform(new StreamSource(baseFractalSvg), 
				              new StreamResult(new BufferedWriter(new FileWriter(outputFractalSvg))));
	}
}
